package com.guanglan.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;

/**
 * Created with IntelliJ IDEA.
 * 作者: 光蓝Comet
 * Date: 2018-06-22 16:06
 * Description: 一句话说明这个类的作用
 * Since 1.0
 */
@ControllerAdvice
public class GlobalHandlerException{

    private static final Logger logger = LoggerFactory.getLogger(GlobalHandlerException.class);

    @ExceptionHandler(value = Exception.class)
    public ModelAndView defaultErrorHandler(HttpServletRequest request,Exception ex){
        logger.error("全局异常捕获堆栈错误轨迹:{},错误信息:{}",ex, ex.getMessage());
        ModelAndView mv = new ModelAndView();
        mv.addObject("error_message",ex.getMessage());
        mv.addObject("error_detail",ex.toString());
        mv.setViewName("exception/exception_page");
        return mv;
    }

    @ExceptionHandler(value = MyException.class)
    public ModelAndView myExceptionErrorHandler(MyException ex){
        logger.error("自定义全局异常捕获堆栈错误轨迹:{},错误信息:{}",ex, ex.getMessage());
        ModelAndView mav = new ModelAndView();
        mav.addObject("error_message",ex.getMessage());
        mav.addObject("error_detail",ex.toString());
        mav.setViewName("exception/exception_page");
        return mav;
    }

}
